package com.mrd.gtimpframe.service.impl;

import com.mrd.exception.SaveException;
import com.mrd.gtimpframe.entity.Attachment;
import com.mrd.gtimpframe.entity.CompanyAttachment;
import com.mrd.gtimpframe.repository.CompanyAttachmentMapper;
import com.mrd.gtimpframe.service.AttachmentService;
import com.mrd.gtimpframe.service.CompanyAttachmentService;
import com.mrd.util.LoginUtil;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.transaction.Transactional;

@Service
public class CompanyAttachmentServiceImpl implements CompanyAttachmentService {
    @Resource
    private CompanyAttachmentMapper companyAttachmentMapper;
    @Resource
    private AttachmentService attachmentService;
    @Value("${file.path}")
    private String fileDir;
    private static final Logger logger = Logger.getLogger(CompanyAttachmentServiceImpl.class);

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return companyAttachmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int insert(CompanyAttachment record) {
        return companyAttachmentMapper.insert(record);
    }

    @Override
    @Transactional
    public int insertSelective(CompanyAttachment record) {
        return companyAttachmentMapper.insertSelective(record);
    }

    @Override
    public CompanyAttachment selectByPrimaryKey(String id) {
        return companyAttachmentMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(CompanyAttachment record) {
        return companyAttachmentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(CompanyAttachment record) {
        return companyAttachmentMapper.updateByPrimaryKey(record);
    }

    /**
     * 文件上传保存方法
     */
    @Override
    @Transactional
    public Attachment saveUploadFile(MultipartFile uploadFile, String type) {
        String name = uploadFile.getOriginalFilename();
        String suffix = "";
        if (name.contains(".")) {
            suffix = name.substring(name.lastIndexOf(".") + 1);
        }
        File file = null;
        Date now = null;
        String newFilePathName = null;
        String newFileName = null;
        while (file == null || file.exists()) {
            now = new Date();
            newFileName = DateFormatUtils.format(now, "yyyyMMddHHmmssSSS");
            newFilePathName = String.format("\\%s\\%s\\%s.%s", type, DateFormatUtils.format(now, "yyyy\\MM\\dd"), newFileName, suffix);
            String filePath = String.format("%s\\%s", fileDir, newFilePathName);
            file = new File(filePath);
        }
        try (InputStream is = uploadFile.getInputStream()) {
            FileUtils.copyToFile(is, file);
            System.out.println(file.getAbsolutePath());
        } catch (IOException e) {
            logger.error("文件上传出错!", e);
            return null;
        }
        Attachment attachment = new Attachment();
        attachment.setId(UUID.randomUUID().toString().replace("-", ""));
        attachment.setCtime(now);
        attachment.setName(name);
        attachment.setReplacename(newFileName);
        attachment.setPath(newFilePathName);
        try {
            attachment.setCreator(LoginUtil.getLoginUser().getId());
        } catch (Exception e) {
        }
        attachmentService.insertSelective(attachment);
        return attachment;
    }
}
